// 创建一个新的 store 实例
import {createStore} from "vuex";
import axios from "axios";
import userModule from "./userModule/index.js";
import empModule from "./empModule/index.js";

const store = createStore({
    state() {
        return {
            count: 0,
            slogan: '桃李不言下自成蹊',
            users: [
                {
                    name: 'user0',
                    gender: 0
                },
                {
                    name: 'user1',
                    gender: 1
                },
                {
                    name: 'user2',
                    gender: 0
                },
                {
                    name: 'user3',
                    gender: 1
                },
                {
                    name: 'user4',
                    gender: 0
                },
            ],
            gender: null,
            regions: [],
            provinces: [],
            cities: [],
            districts: [],
        }
    },
    mutations: {
        increment(state) {
            state.count++
        },
        addNum(state, n) {
            state.count += n
        },
        addCount(state, payload) {
            state.count += payload.amount
        },
        changeGender(state, payload) {
            state.gender = payload.gender;
        },
        updateRegions(state, payload) {
            state.regions = payload.regions;
        },
        updateProvinces(state, payload) {
            state.provinces = payload.provinces;
        },
        updateCities(state, payload) {
            const provinces = state.regions.filter(province => province.regionCode === payload.regionCode);
            let cityList = [];
            provinces[0].cities.forEach(city => {
                cityList.push({
                    regionCode: city.regionCode,
                    regionName: city.regionName,
                })
            });
            state.cities = cityList;
        },
        updateDistricts(state, payload) {
            const provinces = state.regions.filter(province => province.regionCode === payload.provinceCode);
            let districtList = [];
            const cities = provinces[0].cities.filter(city => city.regionCode === payload.cityCode)
            cities[0].districts.forEach(district => {
                districtList.push({
                    regionCode: district.regionCode,
                    regionName: district.regionName,
                })
            });
            state.districts = districtList;
        },
    },
    getters: {
        persons(state) {
            let users = state.users;
            let gender = state.gender;
            return gender != null ? users.filter(user => user.gender === gender) : users;
        }
    },
    actions: {
        async getProvinces({state, getters, commit}) {
            console.log(state.slogan)
            // console.log(getters.persons)
            // commit('increment')
            axios('/area.json').then(res => {
                const regions = res.data;
                commit({
                    type: 'updateRegions',
                    regions
                });
                let provinceList = [];
                regions.forEach(province => {
                    provinceList.push({
                        regionCode: province.regionCode,
                        regionName: province.regionName,
                    });
                });
                commit({
                    type: 'updateProvinces',
                    provinces: provinceList
                });
                // console.log(state.provinces);
            });
        }
    },
    modules: {
        user: userModule,
        emp: empModule
    }
});
export default store